package com.nervenets.web.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.nervenets.utils.JodaUtils;
import com.nervenets.web.entity.KeyValue;
import com.nervenets.web.enumeration.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FlowMessage implements Serializable {
    private int time = JodaUtils.getTimestamp();
    private long userId;
    private FlowType flowType;
    private long unionId;
    private JSONObject data = new JSONObject();

    public FlowMessage(long userId, FlowType flowType, long ip, long unionId, KeyValue... keyValues) {
        this.userId = userId;
        this.flowType = flowType;
        this.addData("ip", ip);
        this.unionId = unionId;
        for (KeyValue kv : keyValues) {
            this.addData(kv.getKey(), kv.getValue());
        }
    }

    public static FlowMessage wealthChange(long userId, long ip, UnionType unionType, long unionId, long offset, OperateType operateType, PayType payType, FlowStatus flowStatus, String detail) {
        FlowMessage message = new FlowMessage(userId, FlowType.wealthChange, ip, unionId);
        message.addData("offset", offset);
        message.addData("operateType", operateType.name());
        message.addData("detail", detail);
        message.addData("payType", payType.name());
        message.addData("unionType", unionType.name());
        message.addData("flowStatus", flowStatus.name());
        message.addData("wealthType", WealthType.money);
        return message;
    }
/*
    public static FlowMessage rewardChange(long userId, long ip, UnionType unionType, long unionId, long offset, OperateType operateType, PayType payType, FlowStatus flowStatus, String detail) {
        FlowMessage message = new FlowMessage(userId, FlowType.rewardChange, ip, unionId);
        message.addData("offset", offset);
        message.addData("operateType", operateType.name());
        message.addData("detail", detail);
        message.addData("payType", payType.name());
        message.addData("unionType", unionType.name());
        message.addData("flowStatus", flowStatus.name());
        return message;
    }

    public static FlowMessage scoreChange(long userId, long ip, UnionType unionType, long unionId, long offset, OperateType operateType, PayType payType, FlowStatus flowStatus, String detail) {
        FlowMessage message = new FlowMessage(userId, FlowType.scoreChange, ip, unionId);
        message.addData("offset", offset);
        message.addData("operateType", operateType.name());
        message.addData("detail", detail);
        message.addData("payType", payType.name());
        message.addData("unionType", unionType.name());
        message.addData("flowStatus", flowStatus.name());
        return message;
    }*/

    public void addData(String key, Object value) {
        this.data.put(key, value);
    }

    public int getIntValue(String key) {
        return this.data.containsKey(key) ? this.data.getIntValue(key) : 0;
    }

    public long getLongValue(String key) {
        return this.data.containsKey(key) ? this.data.getLongValue(key) : 0L;
    }

    public String getStringValue(String key) {
        return this.data.getString(key);
    }
}
